package io.renren.modules.generator.service.impl;

import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import io.renren.common.utils.PageUtils;
import io.renren.modules.generator.dao.TestModuleProjectDao;
import io.renren.modules.generator.entity.TestModuleProjectEntity;
import io.renren.modules.generator.service.TestModuleProjectService;

@Service("testModuleProjectService")
public class TestModuleProjectServiceImpl extends ServiceImpl<TestModuleProjectDao, TestModuleProjectEntity>
        implements TestModuleProjectService {

    @Override
    public List<TestModuleProjectEntity> queryModuleProjectListByModule(String moduleName) {
        return this.baseMapper.queryModuleProjectListByModule(moduleName);
    }

    @Override
    public Page<TestModuleProjectEntity> listTestModuleProject(Page<TestModuleProjectEntity> page,
            @Param("moduleName") String moduleName) {
        return getBaseMapper().listTestModuleProject(page, moduleName);
    }

    @Override
    public Page<TestModuleProjectEntity> queryModuleProjectByProjectName(Page<TestModuleProjectEntity> page,
            @Param("proectName") String projectName) {
        return getBaseMapper().queryModuleProjectByProjectName(page, projectName);
    }

    @Override
    public PageUtils queryModuleProjectPage(Map<String, Object> params) {
        String moduleName = (String) params.get("moduleName");
        String projectName = (String) params.get("projectName");
        if (StringUtils.isNotBlank(moduleName) && StringUtils.isNotBlank(projectName)) {
            Page<TestModuleProjectEntity> page = new Page<>(1, 10);
            // 两个百分号表示模糊匹配
            IPage<TestModuleProjectEntity> ipage = getBaseMapper().queryModuleProjectPage(page, "%" + moduleName + "%",
                    "%" + projectName + "%");
            return new PageUtils(ipage);
        } else if (StringUtils.isNotBlank(moduleName)) {
            Page<TestModuleProjectEntity> page = new Page<>(1, 10);
            IPage<TestModuleProjectEntity> ipage = getBaseMapper().listTestModuleProject(page, "%" + moduleName + "%");
            return new PageUtils(ipage);
        } else if (StringUtils.isNotBlank(projectName)) {
            Page<TestModuleProjectEntity> page = new Page<>(1, 10);
            IPage<TestModuleProjectEntity> ipage = getBaseMapper().queryModuleProjectByProjectName(page,
                    "%" + projectName + "%");
            return new PageUtils(ipage);
        } else {
            Page<TestModuleProjectEntity> page = new Page<>(1, 10);
            IPage<TestModuleProjectEntity> ipage = getBaseMapper().selectModuleProjectList(page);
            return new PageUtils(ipage);
        }
    }
}